package ext.tianma.bom.service.sqe;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import ext.com.core.CoreUtil;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.util.WTException;

public class SQEPartService {

	/**
	 * 获取页面输入的搜索信息并返回要展示的成品信息
	 * @param partNum
	 * @param proNum
	 * @param partSupplier
	 * @return
	 * @throws WTException
	 */
	public static String getShowData(String partNum, String proNum, String partSupplier) throws WTException {

		/*
		 * partNum = partNum.replace("，", ","); String[] nums =
		 * partNum.trim().split(","); String num = "";
		 */
		String str = "";
		String Estr = "";
		List<WTPart> resultList = new ArrayList<WTPart>();
		boolean b = false;

		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		JSONArray Earray = new JSONArray();
		JSONObject Eobject = new JSONObject();

		try {
			long startTime0=System.currentTimeMillis();
			long startTime=System.currentTimeMillis();
			
			if (!SQEPartHelper.isNullOrEmpty(partNum)) {
				System.out.println("-----partNum not null------");
				//查master时用了两行表
				WTPartMaster temp = CoreUtil.getWTPartMasterByNumber(partNum);
				if (temp == null) {
					System.out.println("成品编码查询failed");
					//Earray.put(partNum);
					Eobject.put("status", "failed");
					Eobject.put("datalist", Earray);
					Eobject.put("fly", "");
					Eobject.put("message", "成品编码不存在，请重新输入!");
					Estr = Eobject.toString();
					return Estr;
				} else {
					System.out.println("成品编码查询success");
					ArrayList<WTPart> partList = SQEPartHelper.getAllLatestVersionPart(partNum);
					resultList = partList;
				}
			}
			if (!SQEPartHelper.isNullOrEmpty(proNum)) {
				System.out.println("-----proNum not null------");
				if (resultList.size() == 0) {
					System.out.println("-----proNum result null------");
					ArrayList<WTPart> partList = SQEPartHelper.searchIBA("cn.tianma.projectNumber", proNum);
					if (partList.size() == 0) {
						Eobject.put("status", "failed");
						Eobject.put("datalist", Earray);
						Eobject.put("fly", "");
						Eobject.put("message", "项目编码不存在，请重新输入!");
						Estr = Eobject.toString();
						return Estr;
					} else {
						//partList = SQEPartHelper.removeDuplicate(partList);
						resultList = partList;

					}
				} else {
					System.out.println("-----proNum result not null------");
					WTPart p = new WTPart();
					for (int i = 0; i < resultList.size(); i++) {
						p = resultList.get(i);
						if (!SQEPartHelper.getIBAStringValue(p, "cn.tianma.projectNumber").equals(proNum)) {
							resultList.remove(p);
							i--;
						}
					}

				}

			}
			if (!SQEPartHelper.isNullOrEmpty(partSupplier)) {
				System.out.println("-----supplier not null------"+partSupplier);
				if (resultList.size() == 0) {
					System.out.println("-----proSupplier result null------");
					System.out.println("-----供应商查询 s------");
					
					long startTime2=System.currentTimeMillis();
					ArrayList<WTPart> partList = SQEPartHelper.searchPartSupplier("partSupplier",
							partSupplier);
					long endTime2=System.currentTimeMillis();
					float excTime2=(float)(endTime2-startTime2)/1000;
					System.out.println("------物料查询执行时间：-------"+excTime2+"s");
					
					
					if (partList.size() == 0) {
						System.out.println("-----供应商查询 s------");
						Eobject.put("status", "failed");
						Eobject.put("datalist", Earray);
						Eobject.put("fly", "no");
						Eobject.put("message", "供应商不存在，请重新输入!");
						Estr = Eobject.toString();
						return Estr;
					} else {
						b = true;
						resultList = partList;

					}
				} else {
					System.out.println("-----proSupplier result not null------");
					WTPart p = new WTPart();
					WTPart child = new WTPart();
					List<WTPart> childlist2 = new ArrayList<WTPart>();
					for (int i = 0; i < resultList.size(); i++) {
						p = resultList.get(i);
						List<WTPart> childlist = SQEPartHelper.getChildlist(p.getNumber(), p.getViewName());
						for (WTPart part : childlist) {
							System.out.println("----partNumber-------"+part.getNumber());
							System.out.println("------supplier value------"+SQEPartHelper.getIBAStringValue(part, "partSupplier"));
							//现在的查询结果是半成品和原材料！！     && ("Design".equals(part.getViewName()))
							if ((SQEPartHelper.getIBAStringValue(part, "partSupplier")).indexOf(partSupplier)!=-1) {
								childlist2.add(part);
								System.out.println("-------进入  if---------");
							}
						}

					}
					resultList.clear();
					System.out.println("----after clear-----" + resultList.size());
					resultList = childlist2;
					b = true;
					System.out.println("----after childlist2-----" + childlist2.size());
				}

			}
			
			long endTime=System.currentTimeMillis();
			float excTime=(float)(endTime-startTime)/1000;
			System.out.println("------执行时间：-------"+excTime+"s");
			
			if (resultList.size() == 0) {
				System.out.println("-----resultList=0------");
				Eobject.put("status", "failed");
				Eobject.put("datalist", Earray);
				Eobject.put("fly", "no");
				Eobject.put("message", "不存在符合条件的物料，请重新输入!");
				Estr = Eobject.toString();
				return Estr;
			} else {
				System.out.println("-----b-----" + b);
				if (b == false) {
					array = SQEPartHelper.setData(resultList, array);
					object.put("status", "success");
					object.put("datalist", array);
					object.put("fly", "no");
					object.put("message", "");
					str = object.toString();
				} else {
					array = SQEPartHelper.setData2(resultList, array);
					if(array.length()==0) {
						object.put("status", "failed");
						object.put("datalist", array);
						object.put("fly", "no");
						object.put("message", "不存在符合条件的物料，请重新输入!");
						str = object.toString();
					}else {
						object.put("status", "success");
						object.put("datalist", array);
						object.put("fly", "yes");
						object.put("message", "");
						str = object.toString();
					}
				}
			}

			long endTime0=System.currentTimeMillis();
			float excTime0=(float)(endTime0-startTime0)/1000;
			System.out.println("------try执行时间：-------"+excTime0+"s");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} /*
			 * if (!"".equals(Estr)) { System.out.println("Estr"); return Estr; } else {
			 * System.out.println("str"); return str; }
			 */
		return str;

	}
	
	
	/**
	 * 返回导出Excel时需要的成品信息
	 * @param partNum
	 * @param proNum
	 * @param partSupplier
	 * @return
	 * @throws WTException
	 */
	public static String getExcelData(String partNum, String proNum, String partSupplier) throws WTException {

		String str = "";
		String Estr = "";
		List<WTPart> resultList = new ArrayList<WTPart>();
		boolean b = false;

		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		JSONArray Earray = new JSONArray();
		JSONObject Eobject = new JSONObject();

		try {
			long startTime=System.currentTimeMillis();
			
			if (!SQEPartHelper.isNullOrEmpty(partNum)) {
				WTPartMaster temp = CoreUtil.getWTPartMasterByNumber(partNum);
				if (temp == null) {
					//Earray.put(partNum);
					Eobject.put("status", "failed");
					Eobject.put("path", "");
					Eobject.put("message", "导出失败");
					Estr = Eobject.toString();
					return Estr;
				} else {
					System.out.println("成品编码查询success");
					ArrayList<WTPart> partList = SQEPartHelper.getAllLatestVersionPart(partNum);
					resultList = partList;
				}
			}
			if (!SQEPartHelper.isNullOrEmpty(proNum)) {
				if (resultList.size() == 0) {
					ArrayList<WTPart> partList = SQEPartHelper.searchIBA("cn.tianma.projectNumber", proNum);
					if (partList.size() == 0) {
						Eobject.put("status", "failed");
						Eobject.put("path", "");
						Eobject.put("message", "导出失败");
						Estr = Eobject.toString();
						return Estr;
					} else {
						resultList = partList;

					}
				} else {
					WTPart p = new WTPart();
					for (int i = 0; i < resultList.size(); i++) {
						p = resultList.get(i);
						if (!SQEPartHelper.getIBAStringValue(p, "cn.tianma.projectNumber").equals(proNum)) {
							resultList.remove(p);
							i--;
						}
					}

				}

			}
			if (!SQEPartHelper.isNullOrEmpty(partSupplier)) {
				if (resultList.size() == 0) {
					
					ArrayList<WTPart> partList = SQEPartHelper.searchPartSupplier("partSupplier",
							partSupplier);
					if (partList.size() == 0) {
						Eobject.put("status", "failed");
						Eobject.put("path", "");
						Eobject.put("message", "导出失败");
						Estr = Eobject.toString();
						return Estr;
					} else {
						b = true;
						resultList = partList;

					}
				} else {
					WTPart p = new WTPart();
					WTPart child = new WTPart();
					List<WTPart> childlist2 = new ArrayList<WTPart>();
					for (int i = 0; i < resultList.size(); i++) {
						p = resultList.get(i);
						List<WTPart> childlist = SQEPartHelper.getChildlist(p.getNumber(), p.getViewName());
						for (WTPart part : childlist) {
							//现在的查询结果是半成品和原材料！！     && ("Design".equals(part.getViewName()))
							if ((SQEPartHelper.getIBAStringValue(part, "partSupplier")).indexOf(partSupplier)!=-1) {
								childlist2.add(part);
							}
						}

					}
					resultList.clear();
					resultList = childlist2;
					b = true;
				}

			}
			
			
			if (resultList.size() == 0) {
				Eobject.put("status", "failed");
				Eobject.put("path", "");
				Eobject.put("message", "导出失败");
				Estr = Eobject.toString();
				return Estr;
			} else {
				if (b == false) {
					array = SQEPartHelper.setData(resultList, array);
					object.put("status", "failed");
					object.put("path", "");
					object.put("message", "");
					str = object.toString();
				} else {
					array = SQEPartHelper.setData2(resultList, array);
					if(array.length()==0) {
						object.put("status", "failed");
						object.put("path", "");
						object.put("message", "导出失败");
						str = object.toString();
					}else {
						long startTime2=System.currentTimeMillis();
						String path = SQEWriteExcel.exportExcel(array, null);
						long endTime2=System.currentTimeMillis();
						float excTime=(float)(endTime2-startTime2)/1000;
						System.out.println("------excel执行时间：-------"+excTime+"s");
						object.put("status", "success");
						object.put("path", path);
						object.put("message", "导出成功");
						str = object.toString();
					}
				}
			}

			long endTime=System.currentTimeMillis();
			float excTime=(float)(endTime-startTime)/1000;
			System.out.println("------excel总执行时间：-------"+excTime+"s");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return str;
	}
}
